////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 
// 
// This file is part of the Microsoft .NET Micro Framework Porting Kit Code Samples and is unsupported. 
// Copyright (C) Microsoft Corporation. All rights reserved. Use of this sample source code is subject to 
// the terms of the Microsoft license agreement under which you licensed this sample source code. 
// 
// THIS SAMPLE CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// 
// 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <cores\arm\include\cpu.h>
#include "AT91_USART.h"
#include "XR16L78X_USART.h"
#include "..\usart_options.h"

// channel 0:	(COM1) AT91_DEBUG 
// channel 1:	(COM2) AT91_UART_0, used for NFC device
// channel 2:	(COM3) AT91_UART_1, used for KNX/Lon module

// channel 3:	(COM4) XR16L784 0, used for Bluetooth module	
// channel 4:	(COM5) XR16L784 1, used for RAILBUS (RS485)
// channel 5:	(COM6) XR16L784 2, used for USER RS485
// channel 6:	(COM7) XR16L784 3, used for USER RS232 COM PORT

BOOL CPU_USART_Initialize( int ComPortNum, int BaudRate, int Parity, int DataBits, int StopBits, int FlowValue )
{
	if (ComPortNum < FIRST_EXTERN_CHANNEL) {
	    return AT91_USART_Driver::Initialize(ComPortNum, BaudRate, Parity, DataBits, StopBits, FlowValue);
	} else {
		FlowValue = (ComPortNum == 5 || ComPortNum == 4) ? USART_FLOW_HALF_DUPLEX : FlowValue;
		return XR16L78X_USART_Driver::Initialize(ComPortNum, BaudRate, Parity, DataBits, StopBits, FlowValue);
	}
}

BOOL CPU_USART_Uninitialize( int ComPortNum )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		return AT91_USART_Driver::Uninitialize(ComPortNum);
	else
		return XR16L78X_USART_Driver::Uninitialize(ComPortNum);
}

BOOL CPU_USART_TxBufferEmpty( int ComPortNum )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		return AT91_USART_Driver::TxBufferEmpty(ComPortNum);
	else 
		return XR16L78X_USART_Driver::TxBufferEmpty(ComPortNum);	
}

BOOL CPU_USART_TxShiftRegisterEmpty( int ComPortNum )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		return AT91_USART_Driver::TxShiftRegisterEmpty( ComPortNum );
	else
		return XR16L78X_USART_Driver::TxShiftRegisterEmpty( ComPortNum );
}

void CPU_USART_WriteCharToTxBuffer( int ComPortNum, UINT8 c )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		AT91_USART_Driver::WriteCharToTxBuffer( ComPortNum, c );
	else
		XR16L78X_USART_Driver::WriteCharToTxBuffer( ComPortNum, c );
}

void CPU_USART_TxBufferEmptyInterruptEnable( int ComPortNum, BOOL Enable )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		AT91_USART_Driver::TxBufferEmptyInterruptEnable( ComPortNum, Enable );
	else
		XR16L78X_USART_Driver::TxBufferEmptyInterruptEnable( ComPortNum, Enable );
}

BOOL CPU_USART_TxBufferEmptyInterruptState( int ComPortNum )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		return AT91_USART_Driver::TxBufferEmptyInterruptState( ComPortNum );
	else
		return XR16L78X_USART_Driver::TxBufferEmptyInterruptState( ComPortNum );
}

void CPU_USART_RxBufferFullInterruptEnable( int ComPortNum, BOOL Enable )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		AT91_USART_Driver::RxBufferFullInterruptEnable( ComPortNum, Enable );
	else
		XR16L78X_USART_Driver::RxBufferFullInterruptEnable( ComPortNum, Enable );
}

BOOL CPU_USART_RxBufferFullInterruptState( int ComPortNum )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		return AT91_USART_Driver::RxBufferFullInterruptState( ComPortNum );
	else
		return XR16L78X_USART_Driver::RxBufferFullInterruptState( ComPortNum );
}

BOOL CPU_USART_TxHandshakeEnabledState( int ComPortNum )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
	    return AT91_USART_Driver::TxHandshakeEnabledState( ComPortNum );
	else
	    return XR16L78X_USART_Driver::TxHandshakeEnabledState( ComPortNum );
}

void CPU_USART_ProtectPins( int ComPortNum, BOOL On )
{
   if (ComPortNum < FIRST_EXTERN_CHANNEL) 
		AT91_USART_Driver::ProtectPins( ComPortNum, On );
	else
		XR16L78X_USART_Driver::ProtectPins( ComPortNum, On );
}

UINT32 CPU_USART_PortsCount()
{
    return TOTAL_USART_PORT;
}

void CPU_USART_GetPins( int ComPortNum, GPIO_PIN& rxPin, GPIO_PIN& txPin,GPIO_PIN& ctsPin, GPIO_PIN& rtsPin )
{   
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
	    return AT91_USART_Driver::GetPins( ComPortNum, rxPin, txPin, ctsPin, rtsPin );
	else
	    return XR16L78X_USART_Driver::GetPins( ComPortNum, rxPin, txPin, ctsPin, rtsPin );
}

BOOL CPU_USART_SupportNonStandardBaudRate (int ComPortNum )
{
    return TRUE;
}

void CPU_USART_GetBaudrateBoundary( int ComPortNum, UINT32& maxBaudrateHz, UINT32& minBaudrateHz )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
	    return AT91_USART_Driver::BaudrateBoundary( ComPortNum, maxBaudrateHz, minBaudrateHz );
	else
	    return XR16L78X_USART_Driver::BaudrateBoundary( ComPortNum, maxBaudrateHz, minBaudrateHz );
}

BOOL CPU_USART_IsBaudrateSupported( int ComPortNum, UINT32& BaudrateHz )
{
    if (ComPortNum < FIRST_EXTERN_CHANNEL) 
	    return AT91_USART_Driver::IsBaudrateSupported( ComPortNum, BaudrateHz );
	else
	    return XR16L78X_USART_Driver::IsBaudrateSupported( ComPortNum, BaudrateHz );
}

BOOL CPU_USART_SetOption(int ComPortNum, int Feature, UINT32 Value)
{
	if (ComPortNum >= FIRST_EXTERN_CHANNEL) 
		return XR16L78X_USART_Driver::SetOption( ComPortNum, Feature, Value );
	
	return false;
}

BOOL CPU_USART_GetOption(int ComPortNum, int Feature, UINT32 *Value)
{
	if (ComPortNum >= FIRST_EXTERN_CHANNEL) 
		return XR16L78X_USART_Driver::GetOption( ComPortNum, Feature, Value );
	
	return false;
}

unsigned CPU_USART_WriteBurstToTxBuffer( int ComPortNum,  UINT8 *data, unsigned len )
{
	if (ComPortNum >= FIRST_EXTERN_CHANNEL) 
		return XR16L78X_USART_Driver::WriteBurstToTxBuffer( ComPortNum, data, len );
	
	return len;
}



